﻿using HamcoWcs.Tools.LogCleaner.Entities;
using Models;
using System;

namespace HamcoWcs.Tools.LogCleaner
{
    internal partial class Program
    {
        private static void DeleteDeviceErrorLog(DateTime dateThreshold)
        {
            try
            {
                var a = SQLCLIENT.Deleteable<DeviceErrorLog>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} DeviceErrorLog",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }
            }
            catch (Exception)
            {

            }
        }

        private static void DeleteOperationLog(DateTime dateThreshold)
        {
            try
            {
                var a = SQLCLIENT.Deleteable<OperationLog>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} OperationLog",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }

            }
            catch (Exception)
            {

            }
        }

        private static void DeleteWcsRunLog(DateTime dateThreshold)
        {
            try
            {
                var a = SQLCLIENT.Deleteable<WcsRunLog>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} WcsRunLog",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }
            }
            catch (Exception)
            {

            }
        }

        private static void DeleteSystemErrorLog(DateTime dateThreshold)
        {
            try
            {
                var a = SQLCLIENT.Deleteable<SystemErrorLog>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} SystemErrorLog",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }
            }
            catch (Exception)
            {

            }
        }

        private static void DeleteMesMsgQueueBak()
        {
            DateTime dateThreshold = DateTime.Now.AddDays(-7);

            try
            {
                var a = SQLCLIENT.Deleteable<MesMsgQueueBak>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} MesMsgQueueBak",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }
            }
            catch (Exception)
            {

            }
        }

        private static void DeleteTask(DateTime dateThreshold)
        {
            try
            {
                var a = SQLCLIENT.Deleteable<SystemErrorLog>().Where(x => x.CreatedTime < dateThreshold).ExecuteCommand();
                if (a > 0)
                {
                    SQLCLIENT.Insertable(new SystemErrorLog()
                    {
                        Log = $"LogCleaner Server:[INFO],Delete {a} SystemErrorLog",
                        CreatedTime = DateTime.Now,
                    }).ExecuteCommand();
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
